﻿using System.Xml.Serialization;
using System.IO;
using Microsoft.Xna.Framework;

namespace Imagine
{
        /// <summary>
        /// Variables in this class will be serialized
        /// into XML and deserialized when game loads
        /// Used for game Options
        /// </summary>
        public struct GameInitParams
        {
            //TODO : Add variables here 
            public Point Resolution;
            public bool VerticalSync;
            public bool MultiSample;
            public bool FullScreen;
            public string[] CMDParams;
            
        }

        /// <summary>
        /// Variables in this class will be serialized
        /// into XML and deserialized when game loads
        /// Used for Player Controls
        /// </summary>
        public class GameControls
        {
            //TODO : Add Player Control Variables here
        }

        /// <summary>
        /// This class handles reading of XMl files and initializing
        /// GameConfig classes and also saves those variables
        /// into correspondning XML files
        /// </summary>
        class XmlConfig<T>
        {
            //Generic Function will work for any Damn class out there
            
            /// <summary>
            /// Loads(Deserializes) any varibale form the XML
            /// </summary>
            public static T Load(string xmlPath)
            {
                T xmlObject;
                //read the Document
                using (FileStream fs = File.OpenRead(xmlPath))
                {
                    //Create the Serializer
                    XmlSerializer serializer = new XmlSerializer(typeof(T));

                    //now Deserialize it, we also need to typecast it
                    xmlObject = (T)serializer.Deserialize(fs);
                }
                return xmlObject;
            }

            /// <summary>
            /// Saves(Serializes) any Class Type into the XML
            /// </summary>
            public static void Save(T obj, string path)
            {
                //Always using with FileStreams, so that they will be automatically disposed
                using (FileStream fs = File.OpenWrite(path))
                {
                    //Create the Serializer
                    XmlSerializer serializer = new XmlSerializer(typeof(T));

                    //Now Write Serialized data into XML

                    serializer.Serialize(fs, obj);
                }     
            }

        }//XmlConfig Ends

}//Imagine namespace Ends